package interviewpreparationkit.array

/**
 * [New Year Chaos](https://www.hackerrank.com/challenges/new-year-chaos)
 */
// Complete the minimumBribes function below.
fun minimumBribes(q: Array<Int>): Unit {
    var ans = 0
    var temp: Int
    q.forEachIndexed { index, i ->
        temp = i - index - 1
        if (temp > 2) {
            println("Too chaotic")
            return
        }
        val start = if (i > 2) {
            i - 2
        } else {
            0
        }
        for (j in start until index) {
            if (q[j] > i) {
                ans++
            }
        }
    }
    println(ans)
}